package Thread;

public class ThreadDemo22 {
    public static void main(String[] args) {
        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(()->{
            //sleep一下，是为了给t2时间，让t2也获取到锁
            synchronized (A){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //不释放A尝试获取B
                synchronized (B){
                    System.out.println("t1获得了两把锁");
                }
            }
        });
        Thread t2 = new Thread(()->{
        synchronized (A){
            //给t1时间，让t1也获取到锁
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //尝试获取A，没有释放B
            synchronized (B){
                System.out.println("t2获得了两把锁");
            }
        }
        });
        t1.start();
        t2.start();
    }
}
